package com.blog.controller;

import com.blog.pojo.Comment;
import com.blog.service.ArticleService;
import com.blog.service.CommentService;
import com.blog.service.impl.ArticleServiceImpl;
import com.blog.service.impl.CommentServiceImpl;
import com.blog.utils.DateUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 文章评论
 */
@WebServlet("/CommonServlet.do")
public class CommontServlet extends HttpServlet {

    CommentService commentService = new CommentServiceImpl();
    ArticleService articleService = new ArticleServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //处理请求乱码
        req.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=utf-8");
        String id = req.getParameter("id");
        String w_nickname = req.getParameter("w_nickname");
        String w_content = req.getParameter("w_content");
        //  public Comment(int id, int article_id, String nickname, String content, String time, int star, int diss) {
        Comment comment = new Comment(0, Integer.valueOf(id), w_nickname, w_content, DateUtils.now(), 0, 0);
        int count = commentService.add(comment);
        if(count>0){
            //评论数加一
            articleService.updateCommentCount("+",id);
            //向浏览器响应一段文本
            resp.getWriter().write("<script>alert('评论成功！');location.href='/Blog/ArticleDetailServlet.do?id="+id+"';</script>");
        }else{
            //向浏览器响应一段文本
            resp.getWriter().write("<script>alert('评论异常！');location.href='/Blog/ArticleDetailServlet.do?id="+id+"';</script>");
        }

    }
}
